<?php
declare(strict_types=1);

namespace Service\MessageService;
use Service\BaseService;
use App\Model\BbcModel\Host;
use Service\Code\Normal;

/**
 * 支付回调服务
 * @author chenshubo
 */
class PayNotifyService extends BaseService
{
    /**
     * 支付类服务路径
     * @var string
     */
    protected $payer_path = "Service\\PayService\\Payer\\";
    
    /**
     * 获取回调地址
     * @param array $msg
     */
    public function getChannelNotifyHost(int $cid,string $tob='notify')
    {
        $host = Host::instance()->info(['tob'=>$tob,'df'=>0,'status'=>1]);
        return $host['host'] ?? '';
    }
    
    /**
     * 支付信息回调请求服务
     * @param string $payway
     * @param string $order_sn
     * @param mixed $body
     */
    public function getPayerNotify(int $cid,int $mn_sid,int $mnid,string $payway,string $order_sn,$body)
    {
        $payer = $this->payer_path . ucfirst($payway);
        if( !class_exists($payer) ) {
            return Normal::returnCode(Normal::FAIL,"支付类{$payer}不存在");
        }
        
        $_parsed_body = $payer::instance()->getNotify($body);
        
        //TODO $_parsed_body 解析对应支付公司回调的报文
        //如果支付回调成功，则上报支付单申报动作
        if( $_parsed_body['code'] == Normal::SUC ) 
        {
            $message = [
                'cid'           => $cid,
                'mn_sid'        => $mn_sid,
                'mnid'          => $mnid,
                'pay_way'       => $payway,
                'order_sn'      => $order_sn,
                'pay_notify'    => $body
            ];
            PayDeclareMessageService::instance()->publishMsg($message);
        }
    }
}